<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" session="true" %>
<%
	HttpSession actualSession = request.getSession(true);
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin template="/Templates/WhereIWas.dwt.jsp" codeOutsideHTMLIsLocked="false" -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<!-- InstanceBeginEditable name="doctitle" -->
<title>Where I Was - Index</title>
<!-- InstanceEndEditable -->
<link type="text/css" rel="stylesheet" href="/stylesheets/main.css"/>
<!-- InstanceBeginEditable name="head" -->
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<script type="text/javascript" src="https://maps.google.com/maps/api/js?sensor=false"></script>
<script language="JavaScript" src="javascript/log_registry.js" type="text/javascript"></script>
<script language="JavaScript" src="javascript/location_recognition.js" type="text/javascript"></script>
<script language="JavaScript" src="javascript/label.js" type="text/javascript"></script>
<link rel="stylesheet" type="text/css" href="JS2Cal/css/jscal2.css" />
<link rel="stylesheet" type="text/css" href="JS2Cal/css/border-radius.css" />
<link rel="stylesheet" type="text/css" href="JS2Cal/css/gold/gold.css" />
<script type="text/javascript" src="JS2Cal/js/jscal2.js"></script>
<script type="text/javascript" src="JS2Cal/js/lang/en.js"></script>
<!-- InstanceEndEditable -->
</head>
<!-- InstanceBeginEditable name="bodytag" -->
<body onload="initializeLocator()" >
</body>
<!-- InstanceEndEditable -->
<h1> Where I Was </h1>
<div id="centro">
	<div id="Menu">
			<div id="opciones"><table>
<%
				if(session.getAttribute("LoggedIn") != null){
%>
   					<tr> <td></td><td><a href="/view_log.jsp">View Log</a></td> <td></td><td></td><td></td><td></td>
<%
	}
%>
                    <td></td><td><a href="/help.jsp">View Where I Was Info</a><td><td></td><td></td><td></td>
                    <td></td><td colspan="3"><%= (String)session.getAttribute("UserName") %></td><td></td><td></td><td></td>
                    <td></td><td><a href="/register_location.jsp">Register Location</a><td></td><td></td><td></td><td></td><td></td><td></td>
                    <td></td></td><td><a href="/register_device.jsp">Register a Device</a></td><td></td><td></td><td></td><td></td>
                    <td></td><td></td><td colspan="3"><a href="/log_out">Log Out</a></td><td></td></tr>	
                </table>
	</div>
</div><!-- Opciones-->
</div><!-- Menu-->


<div id="LoginDiv">
<%
	if(session.getAttribute("LoggedIn") != null){
	boolean isLoggedIn = (Boolean)session.getAttribute("LoggedIn");
		if(isLoggedIn){
%>
	<table>
		<tr><td colspan="3"><%= (String)session.getAttribute("UserName") %></td></tr>
		<tr><td><a href="/register_location.jsp">Register Location</a></td><td><a href="/register_device.jsp">Register a Device</a></td></tr>
		<tr><td colspan="3"><a href="/log_out">Log Out</a></td></tr>
	</table>
<%
		}
	}else{
%>
	<form action="/login" method="post">
    	<table>
        	<tr>
            	<td>Username</td>
                <td><input type="text" name="username" /></td>
                <td>Password</td>
                <td><input type="password" name="password" /></td>
            </tr>
            <tr>
				<td><input type="submit" value="LogIn" /></td>
                <td><input type="reset" value="Restore Fields" /></td>
                <td colspan="2"><a href="/registration.jsp">Register a User</a></td>
            </tr>
        </table>
    </form>
<%
	}
%>
</div>

<div id="centro">
<!-- InstanceBeginEditable name="Body" -->
<div id="CalendarDiv"></div>
<%
	Calendar actualCalendar = Calendar.getInstance();
	int year=-1,month=-1,day=-1;
	if(request.getParameter("Day") == null || request.getParameter("Month") == null || request.getParameter("Year") == null){
		Date actualDate = new Date();
		actualCalendar.setTime(actualDate);
		year = actualCalendar.get(Calendar.YEAR);
		day = actualCalendar.get(Calendar.DAY_OF_MONTH);
		month = actualCalendar.get(Calendar.MONTH);
	}else{
		String yearString = request.getParameter("Year");
		year = Integer.parseInt(yearString);
		String monthString = request.getParameter("Month");
		month = Integer.parseInt(monthString);
		String dayString = request.getParameter("Day");
		day = Integer.parseInt(dayString);
	}
	actualCalendar.set(Calendar.YEAR,year);
	actualCalendar.set(Calendar.DAY_OF_MONTH,day);
	actualCalendar.set(Calendar.MONTH,month);
%>
<script language="JavaScript" type="text/javascript">
	var actualDate = new Date(<%=year%>,<%=month%>,<%=day%>);
	var dateNumber = Calendar.dateToInt(actualDate);
	var calendar = Calendar.setup({
		cont:"CalendarDiv",
		date:dateNumber,
		selectionType:Calendar.SEL_SINGLE,
		onSelect:function(){
			var selectedDate = this.selection.get();
			selectedDate = Calendar.intToDate(selectedDate);
			var dayMonth = selectedDate.getDate();
			var month = selectedDate.getMonth();
			var year = selectedDate.getFullYear();
			window.location = "../view_log.jsp?Day="+dayMonth+"&Month="+month+"&Year="+year+"";
		}});
</script>
<%
	GPSLogManager logManager = new GPSLogManager();
	String appUser = (String)session.getAttribute("UserName");
	ArrayList<GPSLogEntry> logList = logManager.getUserLogRegister(appUser,actualCalendar.getTime());
	SimpleDateFormat dateFormat = new SimpleDateFormat("EEE-MM-dd HH:mm");
	LocationManager locationManager = new LocationManager();
	ArrayList<Location> locationList = locationManager.getLocationsList(new AppUser(appUser,null,null,null));
%>
<script language="JavaScript" type="text/javascript">
<%
	for(int i=0;i<locationList.size();i++){
		Location actualLoc = locationList.get(i);
		double lat = actualLoc.getLatitude();
		double lng = actualLoc.getLongitude();
		double radius = actualLoc.getErrorRadius();
		String name = actualLoc.getLocationName();
%>
		addLocationToList(<%=lat%>,<%=lng%>,<%=radius%>,'<%=name%>',<%=i%>)
<%
	}
%>
<%
	for(int i=0;i<logList.size();i++){
		GPSLogEntry logEntry = logList.get(i);
		double lat = logEntry.getLatitude();
		double lng = logEntry.getLongitude();
%>		
		addVisitedLocationToList(<%=lat%>,<%=lng%>,<%=i%>);
<%
	}
%>
</script>
<script language="JavaScript" type="text/javascript">
function toggleShowLocations(value){
	if(value){
		showAllRegisteredLocations();
	}else{
		hideAllRegisteredLocations();
	}
}

function toggleShowLocation(checked,position){
	if(checked){
		showVisitedLocation(position);
	}else{
		hideVisitedLocation(position);
	}
}
</script>
<div id="LogTable">
<form name="visitedLocations" id="visitedLocations">
<table>
<tr><td colspan="5">Location Log</td></tr>
<tr><td>Register Date</td><td>Location</td><td>Device Name</td></tr>
<%
	for(int i=0;i<logList.size();i++){
		GPSLogEntry logEntry = logList.get(i);
		String date = dateFormat.format(logEntry.getLogEntryDate());
		double lat = logEntry.getLatitude();
		double lng = logEntry.getLongitude();
		String deviceName = logEntry.getDeviceName();
		String name = "checkBox" + i;
%>
	<tr><td><%= date %></td><td><script>document.writeln(getLocationName(<%=lat%>,<%=lng%>));</script></td><td><%=deviceName%></td><td><input name="<%=name%>" type="checkbox" onclick="toggleShowLocation(document.visitedLocations.<%=name%>.checked,<%=i%>)"/></td></tr>
<%
	}
%>
<tr><td>Show Registered Locations</td><td><input type="checkbox" name="showLocations" onclick="toggleShowLocations(document.visitedLocations.showLocations.checked)"/></td></tr>
</table>
</form>
</div>
<div id="map_canvas">
</div>
<!-- InstanceEndEditable -->
</div>
<div id="PoweredByGoogle">
<img src="https://developers.google.com/appengine/images/appengine-silver-120x30.gif" 
alt="Powered by Google App Engine" />
<div id="PoweredByAndroid">
<img src="http://1.bp.blogspot.com/_1i7EX7a2ELY/THKCOo4GjQI/AAAAAAAAATg/N-j9SfzyGjk/s200/500px-android-logosvg.png" alt="Powered by Android" height="50" width="50"/>
</div>
</div>
</div> <!-- fin cuerpo-->
</body>
<!-- InstanceEnd --></html>